package edu.asselvi.seguros.db;

import java.sql.Connection; 
import edu.asselvi.seguros.exception.SegurosException;


public class DbStart extends DbUtils {
	
	

	public static void criaDb() {
		
		System.out.println("INICIANDO CRIACAO DE BD");
		
		Connection conn = null;
		
		try {
			conn = new ConnectionFactory().getConnection();

			RunSQLScript script = new RunSQLScript(conn);
			
			script.create("db/database_create_tables.sql");
			script.create("db/database_create_sequences.sql");
			
			System.out.println("CRIACAO DE BD CONCLUIDA");
			
		} catch (SegurosException e) {
			System.err.println(e.getMessage());;
		} finally {
			close(conn);
		}
	}
	
	
	public static boolean testeDb()  {

		Connection conn = null;
		try {
			System.out.println("INICIANDO TESTE DE BD");
			
			conn = new ConnectionFactory().getConnection();
			
			System.out.println("CONEXAO  [ OK ]");
			
			RunSQLScript script = new RunSQLScript(conn);
			script.test("db/database_check.sql");
			System.out.println("TABELAS E SEQUENCIAS  [ OK ]");

			
			System.out.println("TESTE DE BD CONCLUIDO");
			
			return false;
			
		} catch (SegurosException e) {
			System.err.println(e.getMessage());
			return true;
			
		} finally {
			close(conn);
		}
	}

	public boolean checaTabelas()  {

		Connection conn = null;
		try {
			conn = new ConnectionFactory().getConnection();
			
			RunSQLScript script = new RunSQLScript(conn);
			script.testWEB("db/database_check_tables.sql");
			
			return true;
			
		} catch (SegurosException e) {
			System.err.println(e.getMessage());
			return false;
			
		} finally {
			close(conn);
		}
	}
	
		
	public boolean checaSequencias()  {

		Connection conn = null;
		try {
			conn = new ConnectionFactory().getConnection();
			
			RunSQLScript script = new RunSQLScript(conn);
			script.testWEB ("db/database_check_sequencias.sql");
			
			return true;
			
		} catch (SegurosException e) {
			System.err.println(e.getMessage());
			return false;
			
		} finally {
			close(conn);
		}
	}
	

	public void criaTabelas() {
		
		Connection conn = null;
		
		try {
			conn = new ConnectionFactory().getConnection();

			RunSQLScript script = new RunSQLScript(conn);
			
			script.createWEB("db/database_create_tables.sql");
			
		} catch (SegurosException e) {
			System.err.println(e.getMessage());;
		} finally {
			close(conn);
		}
	}
	public void criaSequencias() {
		
		Connection conn = null;
		
		try {
			conn = new ConnectionFactory().getConnection();
			
			RunSQLScript script = new RunSQLScript(conn);
			
			script.createWEB("db/database_create_sequences.sql");
			
		} catch (SegurosException e) {
			System.err.println(e.getMessage());;
		} finally {
			close(conn);
		}
	}
}
